/*
 * @(#) MenuServiceImpl.java 2018年1月25日 上午10:53:51
 *
 * Copyright 2018 CIMIP, Inc. All rights reserved.
 * H-VISIONS PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */package cn.hc.share.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.hc.share.dao.MenuDao;
import cn.hc.share.entity.Menu;
import cn.hc.share.entity.MenuTree;
import cn.hc.share.service.MenuService;
import cn.hc.share.util.StringUtil;

@Service("menuService")
public class MenuServiceImpl implements MenuService
{

	@Resource
	private MenuDao menuDao;

	// 增加目录
	@Transactional
	public int addMenu(Menu menu)
	{
		if (menu == null)
		{
			throw new RuntimeException("menu不存在!");
		}

		int num = menuDao.addMenu(menu);// 1-添加成功,不等于1添加失败
		if (num != 1)
		{
			throw new RuntimeException("添加失败!");
		}
		return num;

	}

	// 修改目录名称
	@Transactional
	public int renameMenu(Menu menu)
	{
		if (menu == null)
		{
			throw new RuntimeException("menu不存在!");
		}

		int num = menuDao.renameMenu(menu);// 1-更新成功,不等于1更新失败
		if (num != 1)
		{
			throw new RuntimeException("更新失败!");
		}
		return num;

	}

	// 获取节点下所有的子节点
	@Transactional
	public List<MenuTree> showMenuTree(String id)
	{
		List<MenuTree> childrenList = new ArrayList<MenuTree>();
		if (StringUtil.isNotNull(id))
		{
			List<Map<String, String>> childrenMenu = menuDao.getChildrenMenu(id);
			if (childrenMenu != null && childrenMenu.size() > 0)
			{
				for (int i = 0; i < childrenMenu.size(); i++)
				{
					MenuTree menuTree = new MenuTree();
					String nodeId = childrenMenu.get(i).get("nodeId");
					String text = childrenMenu.get(i).get("text");
					List<Map<String, String>> list = menuDao.getChildrenMenu(nodeId);
					List<MenuTree> list2 = null;
					if (list == null || list.size() == 0)
					{
						menuTree.setState("open");
					} else
					{
						menuTree.setState("closed");
						list2 = showMenuTree(nodeId);

					}

					menuTree.setId(nodeId);
					menuTree.setText(text);
					if (list2 != null)
					{
						menuTree.setChildren(list2);
					}

					childrenList.add(menuTree);
				}
			}
		}

		return childrenList;
	}

	// 获取节点下所有的子节点
	@Transactional
	public List<String> getMenuIds(String id)
	{
		List<String> childrenMenuIds = new ArrayList<String>();
		if (StringUtil.isNotNull(id))
		{
			List<Map<String, String>> childrenMenu = menuDao.getChildrenMenu(id);
			if (childrenMenu != null && childrenMenu.size() > 0)
			{
				for (int i = 0; i < childrenMenu.size(); i++)
				{
					String nodeId = childrenMenu.get(i).get("nodeId");
					childrenMenuIds.add(nodeId);

					List<Map<String, String>> list = menuDao.getChildrenMenu(nodeId);
					List<String> list2 = null;
					if (list == null || list.size() == 0)
					{
					} else
					{
						list2 = getMenuIds(nodeId);
					}

					if (list2 != null)
					{
						childrenMenuIds.addAll(list2);
					}
				}
			}
		}

		return childrenMenuIds;
	}

	// 查找目录
	@Transactional
	public Menu findMenuByNodeId(String nodeId)
	{
		Menu menu = menuDao.findMenuByNodeId(nodeId);
		return menu;
	}

	//删除目录及其所有子目录
	@Transactional
	public int deleteMenus(List<String> menuIds)
	{
		int nums=0;
		for(String id:menuIds) {
			int num = menuDao.deleteMenuById(id);
			if(num==0) {
				System.out.println("id:"+id+";删除失败!");
			}
			nums+=num;
		}
		return nums;
	}

}
